/*****************************************************************************

File:   otkListFeaturesExample.java

Date          Build     Author        Version    Comments
18-Sep-12    P-20-14    dshah          $$1        Created
20-mar-15 P-30-05 gshmelev $$2  called GetCurrentSession


****************************************************************************/

/* 
 
 This example will work on the current model as input and do the following: 
  a) Write to file, failed, child of failed and child of external failed features in the model
  b) Write to file, the status flags for all features in the model

*/

package com.ptc.otkjavaexamples;
 
import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import javax.swing.JOptionPane;
import java.io.*;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcServer.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.wfc.wfcSolid.*;
import com.ptc.wfc.wfcFeatureInstructions.*;
import com.ptc.pfc.pfcSelect.*;
import com.ptc.wfc.wfcFeature.*;

class otkListFeaturesExample extends DefaultUICommandActionListener
{
    
    
    
    public otkListFeaturesExample()
    {
    }
    
    
    
    
    public void OnCommand()
    {
	addListFeaturesExample();
    }
    
    
    public void addListFeaturesExample ()
    {
	try
	    {
		FileWriter file = new FileWriter ("ListFeaturesInfo.txt");
		BufferedWriter fp_out_solid = new BufferedWriter(file);
		
		fp_out_solid.write("- List Features Info Example -");
		fp_out_solid.newLine();
		fp_out_solid.write("==============================");
		fp_out_solid.newLine();
		
		Session session = pfcSession.GetCurrentSession ();
		
		WSolid myWSolid = (WSolid)(session.GetCurrentModel());
		
		if (myWSolid == null)
		    return;
		/**
		   List to file failed, child of failed and 
		   child of external failed features in the model
		*/
		otkListAllFailedFeatures(myWSolid, fp_out_solid );
		
		/**
		   Write to file the status flags for all features in the model 
		*/
		otkGetSolidFeatureStatusFlags(myWSolid, fp_out_solid );
		
		fp_out_solid.close();
		
		return;		
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return;
    }
    
    void otkListAllFailedFeatures ( WSolid myWSolid, BufferedWriter fp_out_solid  )
    {
	Features Failed = null, COFailed = null, COEFailed = null;
	int n_failed = 0, n_co_failed = 0, n_cox_failed = 0;
	boolean NoResMode = false;
	
	try
	    {
		Failed = myWSolid.ListFailedFeatures();
		
		if (Failed != null)
		    {
			n_failed = Failed.getarraysize();
			fp_out_solid.write("\nNumber of Failed Features = " + n_failed );
			fp_out_solid.newLine();
			otkPrintFeatureIds(Failed, fp_out_solid);
		    }
		else
		    {
			fp_out_solid.write("\nNO Failed Features exist");
			fp_out_solid.newLine();
		    }
		
		/** 
		    'Child of Failed' and 'Child of External Failed' features 
		    may exist only if 'No Resolve Mode' (default mode in Creo) 
		    is active. Verifying the mode nonetheless.
		*/
		NoResMode = myWSolid.GetIsNoResolveMode();
		
		if (NoResMode)
		    {		
			COFailed = myWSolid.ListChildOfFailedFeatures();
			
			if (COFailed != null)
			    {
				n_co_failed = COFailed.getarraysize();
				fp_out_solid.write("\nNumber of 'Child of Failed' Features = " + n_co_failed);
				fp_out_solid.newLine();
					otkPrintFeatureIds(COFailed, fp_out_solid);
			    }
			else
			    {
				fp_out_solid.write("\nNO 'Child of Failed' Features exist");
				fp_out_solid.newLine();
			    }
			
			COEFailed = myWSolid.ListChildOfExternalFailedFeatures();
			
			if (COEFailed != null)
			    {
				n_cox_failed = COEFailed.getarraysize();
				fp_out_solid.write("\nNumber of 'Child of External Failed' Features = " + n_cox_failed);
				fp_out_solid.newLine();
				otkPrintFeatureIds(COEFailed, fp_out_solid);
			    }
			else
			    {
				fp_out_solid.write("\nNO 'Child of External Failed' Features exist");
				fp_out_solid.newLine();
			    }
		    }
		return;
		
	    }
	
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return;
    }
    
    void otkPrintFeatureIds (Features Feats, BufferedWriter fp_out_solid )
    {
	try
	    {
		for (int i=0 ; i<Feats.getarraysize() ; i++)
		    {
			int id = (Feats.get(i)).GetId();
			
			fp_out_solid.write("  ++Feature Id = " + id);
			fp_out_solid.newLine();
		    }
			return;
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return;
    }
    
    void otkGetSolidFeatureStatusFlags ( WSolid myWSolid, BufferedWriter fp_out_solid  )
    {
	try
	    {
		FeatureStatusFlags FeatStatFlags = null;
		FeatureStatusFlag ThisFlag = null;
		
		FeatStatFlags = myWSolid.GetSolidFeatureStatusFlags();
		
		fp_out_solid.write("\n\nWriting Feature Status Flags for All Solid Features");
		fp_out_solid.newLine();
		fp_out_solid.write("===================================================");
		fp_out_solid.newLine();
		
		for (int j=0 ; j<FeatStatFlags.getarraysize() ; j++)
		    {
			ThisFlag = FeatStatFlags.get(j);
			fp_out_solid.newLine();
			fp_out_solid.write("For Feature Id: " + ThisFlag.GetFeatureId());
			fp_out_solid.newLine();
		
			if (ThisFlag.GetIsInvalid())
			    {
				fp_out_solid.write("  -Feature Status: Invalid");
				fp_out_solid.newLine();
			    }
			
			if (ThisFlag.GetIsInactive())
			    {
				fp_out_solid.write("  -Feature Status: Inactive");
				fp_out_solid.newLine();
			    }
			
			if (ThisFlag.GetIsActive())
			    {
				fp_out_solid.write("  -Feature Status: Active");
				fp_out_solid.newLine();
			    }
			
			if (ThisFlag.GetIsFamtabSuppressed())
			    {
				fp_out_solid.write("  -Feature Status: Fam Table Suppressed");
				fp_out_solid.newLine();
			    }
			
			if (ThisFlag.GetIsSimprepSuppressed())
			    {
				fp_out_solid.write("  -Feature Status: Simprep Suppressed");
				fp_out_solid.newLine();
			    }
			
			if (ThisFlag.GetIsProgramSuppressed())
			    {
				fp_out_solid.write("  -Feature Status: Program Suppressed");
				fp_out_solid.newLine();
			    }
			
			if (ThisFlag.GetIsSuppressed())
			    {
				fp_out_solid.write("  -Feature Status: Suppressed");
				fp_out_solid.newLine();
			    }
			
			if (ThisFlag.GetIsUnregenerated())
			    {
				fp_out_solid.write("  -Feature Status: Unregenerated");
				fp_out_solid.newLine();
			    }
			
			if (ThisFlag.GetIsFailed())
			    {
				fp_out_solid.write("  -Feature Status: Failed");
				fp_out_solid.newLine();
			    }
		
			if (ThisFlag.GetIsChildOfFailed())
			    {
				fp_out_solid.write("  -Feature Status: Child of Failed");
				fp_out_solid.newLine();
			    }
			
			if (ThisFlag.GetIsChildOfExternalFailed())
			    {
				fp_out_solid.write("  -Feature Status: Child of External Failed");
				fp_out_solid.newLine();
			    }
		    }
		return;
	    }
	catch (Exception e)
	    {
		System.out.println("1: " + e);
	    }
	return;
	
    }
    
};






